/**
 * @file   GetPropertyValueRequestPacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-22
 * @brief  DON packet object: Get Property Value Request
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/Packet/GetPropertyValueRequestPacket.h>
#include <DON/Core/Packet/GenericPacket.h>
#include <DON/Core/DataType/UInt8.h>
#include <DON/Core/DataType/UInt16.h>
#include <DON/Core/DataType/Structure.h>
#include <QtCore/QByteArray>

using namespace DON::Core::Packet;
using namespace DON::Core::DataType;

class GetPropertyValueRequestPacket::Private
{
public:
    Private()
        : propertyId(0u)
    { }

    quint16 propertyId;
};

GetPropertyValueRequestPacket::GetPropertyValueRequestPacket()
    : AbstractPacket(),
      d(new Private())
{
    setPacketType(PacketType_GetPropertyValueRequest);
}

GetPropertyValueRequestPacket::GetPropertyValueRequestPacket(const quint16 source,
                                                             const quint16 destination,
                                                             const quint16 sequence,
                                                             const quint16 propertyId)
    : AbstractPacket(source,
                     destination,
                     PacketType_GetPropertyValueRequest,
                     sequence),
      d(new Private())
{
    d->propertyId = propertyId;
}

GetPropertyValueRequestPacket::GetPropertyValueRequestPacket(
        const GetPropertyValueRequestPacket &other)
    : AbstractPacket(other),
      d(new Private())
{
    d->propertyId = other.d->propertyId;
}

GetPropertyValueRequestPacket::~GetPropertyValueRequestPacket()
{
    delete d;
}

GetPropertyValueRequestPacket & GetPropertyValueRequestPacket::operator=(
        const GetPropertyValueRequestPacket &other)
{
    AbstractPacket::operator=(other);

    d->propertyId = other.d->propertyId;

    return *this;
}

bool GetPropertyValueRequestPacket::isValid() const
{
    return true;
}

quint16 GetPropertyValueRequestPacket::getPropertyId() const
{
    return d->propertyId;
}

void GetPropertyValueRequestPacket::setPropertyId(const quint16 propertyId)
{
    d->propertyId = propertyId;
}

GetPropertyValueRequestPacket GetPropertyValueRequestPacket::fromGenericPacket(
        const GenericPacket &genericPacket)
{
    bool success = false;
    GetPropertyValueRequestPacket packet;

    if (genericPacket.isValid() &&
        (genericPacket.getPacketType() == packet.getPacketType()))
    {
        // Parse payload items
        const Structure *payloadItems = genericPacket.getPayloadItems(&success);

        if (success)
        {
            if (payloadItems->getMemberCount() != 2)
            {
                success = false;
            }
        }

        // Property ID
        quint16 propertyId = 0u;

        if (success)
        {
            const AbstractDataType *adt = payloadItems->getMember(0);
            const UInt16 *value = UInt16::fromAbstractDataType(adt);

            if (value != NULL)
            {
                propertyId = value->getValue();
            }
            else
            {
                success = false;
            }
        }

        delete payloadItems;
        payloadItems = NULL;

        // Save values
        if (success)
        {
            packet.setSource(genericPacket.getSource());
            packet.setDestination(genericPacket.getDestination());
            packet.setSequence(genericPacket.getSequence());

            packet.setPropertyId(propertyId);
        }
    }

    return packet;
}

QList<QByteArray> GetPropertyValueRequestPacket::getPayloadDataItems(bool *ok) const
{
    QList<QByteArray> payloadDataItems;
    bool success = false;

    if (isValid())
    {
        success = true;

        // Property ID
        payloadDataItems.append(UInt16::toBinary(d->propertyId));
    }

    if (ok != NULL)
    {
        *ok = success;
    }

    return payloadDataItems;
}
